package demo
{
	import Box2D.Collision.Shapes.b2CircleShape;
	import Box2D.Collision.Shapes.b2MassData;
	import Box2D.Collision.Shapes.b2PolygonShape;
	import Box2D.Collision.Shapes.b2Shape;
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.Contacts.b2Contact;
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2BodyDef;
	import Box2D.Dynamics.b2FixtureDef;
	
	import demo.event.CollideEvent;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.media.Sound;
	
	public class DemoScene2BodyEvent extends DemoScene1CreateBody
	{
		[Embed(source="res/sound1.mp3")]
		public static var SoundClass_1:Class;
		private var sound:Sound;
		private var contactcallback:ContactCallback;
		
		public function DemoScene2BodyEvent(s:Sprite)
		{
			super(s);
			sound = new SoundClass_1();
			contactcallback = new ContactCallback();
			m_world.SetContactListener(contactcallback);
			
			contactcallback.body = body;
			contactcallback.soundPlay = playSound;
			
			s.stage.addEventListener(MouseEvent.CLICK, clickHandler);
			contactcallback.addEventListener(CollideEvent.COLLIDE, collideHandler);
		}
		
		override protected function run(e:Event):void{
			super.run(e);
			var body:b2Body = m_world.GetBodyList();
			while(body!=null)
			{
				if(body.GetDefinition().userData is Ball)
				{
					var ballSprite:Ball = body.GetDefinition().userData as Ball;
					ballSprite.x = body.GetPosition().x * 30;                              //皮肤同步x,y
					ballSprite.y = body.GetPosition().y * 30;
					ballSprite.rotation = body.GetAngle() * (180 / Math.PI);
				}
				body = body.GetNext();
			}
		}
		
		protected function collideHandler(e:CollideEvent):void{
			var contact:b2Contact = e.contact;
			var bodyA:b2Body = contact.GetFixtureA().GetBody();
			var bodyB:b2Body = contact.GetFixtureB().GetBody();
			
			if(bodyA == body && bodyB.GetUserData() is Ball){
				bodyB.ApplyForce(new b2Vec2(5000, -5000), bodyB.GetWorldCenter());
			} else if(bodyB == body && bodyA.GetUserData() is Ball){
				bodyA.ApplyForce(new b2Vec2(5000, -5000), bodyA.GetWorldCenter());
			}
		}
		private function playSound(body:b2Body):void{
			//播放声音
			sound.play();
		}
		
		protected function clickHandler(e:MouseEvent):void{
			//创建一个角色
			var bodyDefC:b2BodyDef = new b2BodyDef();
			bodyDefC.angle = Math.random() * Math.PI;
			bodyDefC.userData = new Ball();
			bodyDefC.userData.x = s.mouseX;
			bodyDefC.userData.y = 100;
			bodyDefC.position.Set(s.mouseX * m_physScale, 100 * m_physScale);
			bodyDefC.type = b2Body.b2_dynamicBody;
			var circShape:b2CircleShape = new b2CircleShape(getSize(30));
			var fixtureDef:b2FixtureDef = new b2FixtureDef();
			fixtureDef.shape = circShape;
			fixtureDef.density = 5.0;//密度
			fixtureDef.friction = 0.8;//摩擦力
			fixtureDef.restitution = 0.3;//弹力
			fixtureDef.isSensor = false;
			ball = m_world.CreateBody(bodyDefC);
			ball.CreateFixture(fixtureDef);
			
//			var shape:b2CircleShape = new b2CircleShape(getSize(30));
//			shape.SetLocalPosition(new b2Vec2(0, 1.0));
//			ball.CreateFixture2(shape, 1.0);
//			shape.SetLocalPosition(new b2Vec2(0.0, -1.0));
//			ball.CreateFixture2(shape, 1.0);
//			shape.SetLocalPosition(new b2Vec2(-1.0, 0.0));
//			ball.CreateFixture2(shape, 1.0);
//			shape.SetLocalPosition(new b2Vec2(1.0, 0.0));
//			ball.CreateFixture2(shape, 1.0);
			
			s.addChild(ball.GetUserData());
			
			ball.ApplyForce(new b2Vec2(0, 500), ball.GetWorldCenter());
		}
	}
}